<?php

namespace app\admin\app\controller;

use plugin\admin\app\common\Auth;
use plugin\admin\app\model\Admin;
use support\Db;
use support\Request;
use support\Response;
use app\admin\app\model\Card;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 卡密管理
 */
class CardController extends Crud
{

    /**
     * @var Card
     */
    protected $model = null;

    protected $dataLimit = 'personal';

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new Card;
    }

    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('app/card/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $number = $request->post('number');
            $card_number = Admin::where('id', admin_id())->value('card_number');
            if ($card_number > 0) {
                $count = $this->model->where(['admin_id' => admin_id()])->count();
                if ($count + $number > $card_number) {
                    return $this->json(1, '卡密数量超过额度' . $card_number);
                }
            }
            try {
                Db::transaction(function () use ($request, $number) {
                    for ($i = 0; $i < $number; $i++) {
                        $data = $this->insertInput($request);
                        $data['card'] = '';
                        $id = $this->doInsert($data);
                    }
                });
                return $this->json(0, 'ok', []);
            } catch (\Throwable $e) {
                return $this->json(1, '卡密生成失败' . $e->getMessage());
            }
        }
        return view('app/card/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('app/card/update');
    }

    public function unbind(Request $request): Response
    {
        $ids = $this->deleteInput($request);
        if (!$ids) {
            return $this->json(1, '请选择数据');
        }
        $primary_key = $this->model->getKeyName();
        $this->model->whereIn($primary_key, $ids)->each(function ($model) {
            if ($model->unbind < 1) {
                $model->serial_number = '';
                ++$model->unbind;
                $model->end_time = date('Y-m-d H:i:s', strtotime('-3 day', strtotime($model->end_time)));
                $model->save();
            }
        });
        return $this->json(0);
    }

    public function config(Request $request): Response
    {
        $card = Card::find($request->get('id'));
        if ($request->method() === 'POST') {
            $card->config = array_merge($card->config, $request->post());
            $card->save();
            return $this->json(0);
        }
        $config = $card->config;
        return view('app/card/config', ['config' => $config]);
    }
}
